<?php

/**
 * This is the model class for table "tbl_khachhang".
 *
 * The followings are the available columns in table 'tbl_khachhang':
 * @property integer $id
 * @property string $ten
 * @property string $cmnd
 * @property string $dien_thoai
 * @property string $email
 * @property string $dia_chi
 * @property string $ma_so
 * @property integer $tong_tieu_dung
 * @property integer $duoc_chiet_khau
 * @property integer $da_thanh_toan
 * @property integer $chua_thanh_toan
  * @property string $ma_tk_nganhang
 * @property string $thongtin_nganhang
 */
class Khachhang extends CActiveRecord
{
	public $doitac;
	public $so_luong;
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'tbl_khachhang';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('ten, cmnd, dien_thoai, email, dia_chi, ma_so','required','message'=>'{attribute} chưa nhập dữ liệu'),
			array('tong_tieu_dung, duoc_chiet_khau, da_thanh_toan, chua_thanh_toan', 'numerical', 'integerOnly'=>true),
			array('ten', 'length', 'max'=>200),
			// array('ma_so', 'unique', 'message'=>'{attribute} đã được sử dụng'),
			array('cmnd, ma_so', 'length', 'max'=>50),
			array('dien_thoai', 'length', 'max'=>20),
			array('email', 'length', 'max'=>100),
			array('email', 'unique', 'message'=>'{attribute} đã được sử dụng'),
			
			array('email', 'email', 'message'=>'{attribute} không đúng định dạng'),
			array('ma_tk_nganhang, thongtin_nganhang', 'length', 'max'=>250),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('id, ten, cmnd, dien_thoai, email, dia_chi, ma_so, tong_tieu_dung, duoc_chiet_khau, da_thanh_toan, chua_thanh_toan', 'safe', 'on'=>'search'),
		);
	}
	public static function GetSoTienChuaThanhToan($value)
	{
		$model = DoitacKhachhang::model()->findAll("id_khachhang = {$value->id} and trangthai_thanhtoan_kh != 2");
		$tien = 0;
		foreach ($model as $key ) {
			if ($key->chua_thanh_toan_kh > 0) {
				$tien += $key->chua_thanh_toan_kh;
			}
		}
		if ($tien < 0) {
			$tien = 0;
		}
		return $tien;
	}
	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'hoadon'=> array(self::HAS_MANY,'DoitacKhachhang','id_khachhang'),
		);
	}
	public static function getSotienthanhtoan($arrs)
	{
		$tongtien = 0;
		foreach ( $arrs as $key) {
			$hoadon = DoitacKhachhang::model()->findByPk($key);
			if ($hoadon != null) {
				$tongtien += $hoadon->chua_thanh_toan_kh;
			}
		}
		return $tongtien;
	}
	public static function getSotientieudung($arrs)
	{
		$tongtien = 0;
		foreach ( $arrs as $key) {
			$hoadon = DoitacKhachhang::model()->findByPk($key);
			if ($hoadon != null) {
				$tongtien += $hoadon->so_tien;
			}
		}
		return $tongtien;
	}
	public static function getSotienchietkhau($arrs)
	{
		$tongtien = 0;
		foreach ( $arrs as $key) {
			$hoadon = DoitacKhachhang::model()->findByPk($key);
			if ($hoadon != null) {
				$tongtien += $hoadon->chiet_khau;
			}
		}
		return $tongtien;
	}
	public static function createExcel($model)
	{
		if ($model != null) {
				error_reporting(E_ALL);
				ini_set('display_errors', TRUE);
				ini_set('display_startup_errors', TRUE);
				Yii::import('ext.Excel.XPHPExcel');		
				$objPHPExcel = XPHPExcel::createPHPExcel();
				$objPHPExcel->getProperties()->setCreator("Trieu nhu")
											 ->setLastModifiedBy("Trieu nhu")
											 ->setTitle("Trieu nhu")
											 ->setSubject("Trieu nhu")
											 ->setDescription("Trieu nhu.")
											 ->setKeywords("Trieu nhu")
											 ->setCategory("Trieu nhu");
				$objPHPExcel->getActiveSheet()->setCellValue('A1',"Tên thành viên");
				$objPHPExcel->getActiveSheet()->setCellValue('B1',$model->ten);

				$objPHPExcel->getActiveSheet()->setCellValue('A2',"Mã số thành viên");
				$objPHPExcel->getActiveSheet()->setCellValue('B2',$model->ma_so);				
				$objPHPExcel->getActiveSheet()->setCellValue('A3',"Địa chỉ");
				$objPHPExcel->getActiveSheet()->setCellValue('B3',$model->dia_chi);
				$objPHPExcel->getActiveSheet()->setCellValue('A4',"Điện thoại");
				$objPHPExcel->getActiveSheet()->setCellValue('B4',$model->dien_thoai);
				$objPHPExcel->getActiveSheet()->setCellValue('A5',"Email");
				$objPHPExcel->getActiveSheet()->setCellValue('B5',$model->email);
				$objPHPExcel->getActiveSheet()->setCellValue('A6',"Tổng tiêu dùng");
				$objPHPExcel->getActiveSheet()->setCellValue('B6',getMoney($model->tong_tieu_dung));
				$objPHPExcel->getActiveSheet()->setCellValue('A7',"Tổng chiết khấu");
				$objPHPExcel->getActiveSheet()->setCellValue('B7',getMoney($model->duoc_chiet_khau));
				$objPHPExcel->getActiveSheet()->setCellValue('A8',"Đã thanh toán");
				$objPHPExcel->getActiveSheet()->setCellValue('B8',getMoney($model->da_thanh_toan));
				$objPHPExcel->getActiveSheet()->setCellValue('A9',"Chưa thanh toán");
				$objPHPExcel->getActiveSheet()->setCellValue('B9',getMoney($model->chua_thanh_toan));
				$objPHPExcel->getActiveSheet()->setCellValue('A9',"Mã tài khoản ngân hàng");
				$objPHPExcel->getActiveSheet()->setCellValue('B9',getMoney($model->ma_tk_nganhang));
				$objPHPExcel->getActiveSheet()->setCellValue('A9',"Thông tin ngân hàng");
				$objPHPExcel->getActiveSheet()->setCellValue('B9',getMoney($model->thongtin_nganhang));
						// Set active sheet index to the first sheet, so Excel opens this as the first sheet
				$objPHPExcel->setActiveSheetIndex(0);
				$objPHPExcel->getActiveSheet()
						    ->getColumnDimension('A')
						    ->setWidth(20);
				$objPHPExcel->getActiveSheet()
						    ->getColumnDimension('B')
						    ->setWidth(20);
				$filename = toSlug(stripVietnamese($model->ten)).'-'.strtotime(date("Y-m-d H:i:s")).'.xls';
				header('Content-Type: application/vnd.ms-excel');
				header("Content-Disposition: attachment;filename={$filename}");
				header('Cache-Control: max-age=0');

				$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
				$objWriter->save('php://output');
		}
	}
	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'ten' => 'Tên thành viên',
			'cmnd' => 'Chứng minh nhân dân',
			'dien_thoai' => 'Điện thoại',
			'email' => 'Email',
			'dia_chi' => 'Địa chỉ',
			'ma_so' => 'Mã số thành viên',
			'tong_tieu_dung' => 'Tổng tiêu dùng',
			'duoc_chiet_khau' => 'Được chiết khấu',
			'da_thanh_toan' => 'Đã thanh toán',
			'chua_thanh_toan' => 'Chưa thanh toán',
			'ma_tk_nganhang' =>'Mã tài khoản ngân hàng',
			'thongtin_nganhang'=>'Thông tin ngân hàng',
			'doitac'=>"Đối tác",
			'so_luong'=>"Số lượng"
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('ten',$this->ten,true);
		$criteria->compare('cmnd',$this->cmnd,true);
		$criteria->compare('dien_thoai',$this->dien_thoai,true);
		$criteria->compare('email',$this->email,true);
		$criteria->compare('dia_chi',$this->dia_chi,true);
		$criteria->compare('ma_so',$this->ma_so,true);
		$criteria->compare('tong_tieu_dung',$this->tong_tieu_dung);
		$criteria->compare('duoc_chiet_khau',$this->duoc_chiet_khau);
		$criteria->compare('da_thanh_toan',$this->da_thanh_toan);
		$criteria->compare('chua_thanh_toan',$this->chua_thanh_toan);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
			'pagination'=>array(
	            'pageSize'=>30,
	        ),
		));
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return Khachhang the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
}
